package admin

import (
	"encoding/json"
	"fmt"
	"gitee.com/yangguoliang001/go-gin-vue3/QO"
	"gitee.com/yangguoliang001/go-gin-vue3/pkg/response"
	"gitee.com/yangguoliang001/go-gin-vue3/util/code"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
)

// QueryAdminByUsername 查询用户信息
// @Summary 查询用户信息
// @Description 查询用户信息desc
// @Tags API.admin
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param username formData string true "用户名"
// @Success 200 {object} response.TResponse{data=vo.AdminVO}
// @Failure 400 {object} response.TResponse{data=interface{}}
// @Router /api/admin/info [post]
// @Security LoginToken
func (ac *adminController) QueryAdminByUsername(c *gin.Context) {
	username := c.PostForm("username")
	if username == "" {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{})
		return
	}
	user, err := ac.adminService.QueryAdminByUsername(c, username)
	if err == nil {
		response.Success(c, "请求数据成功", gin.H{
			"id":       user.Id,
			"username": user.Username,
			"nickname": user.Nickname,
			"mobile":   user.Mobile,
		})
	}
}

// QueryAdminList 管理员列表
// @Summary 管理员列表
// @Description 管理员列表
// @Tags API.admin
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param page query int true "第几页" default(1)
// @Param pageSize query int true "每页显示条数" default(10)
// @Param username query string false "用户名"
// @Param nickname query string false "昵称"
// @Param mobile query string false "手机号"
// @Success 200 {object} response.TResponse{data=vo.AdminListVO}
// @Failure 400 {object} response.TResponse{data=interface{}}
// @Router /api/admin [get]
// @Security LoginToken
func (ac *adminController) QueryAdminList(c *gin.Context) {
	var adminListReq QO.AdminListReq
	if err := c.ShouldBind(&adminListReq); err != nil {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": err})
		return
	}
	adminListRes, err := ac.adminService.QueryAdminList(c, adminListReq)

	j, _ := json.Marshal(adminListRes)
	adminListMap := make(map[string]interface{})
	_ = json.Unmarshal(j, &adminListMap)

	if err == nil {
		response.Success(c, "获取管理员列表数据成功", adminListMap)
	}
}

// AddAdmin 新增管理员
// @Summary 新增管理员
// @Description 新增管理员
// @Tags API.admin
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param username formData string true "用户名"
// @Param nickname formData string true "昵称"
// @Param mobile formData string true "手机号"
// @Param password formData string true "MD5后的密码"
// @Success 200 {object} response.TResponse{data=interface{}}
// @Failure 400 {object} response.TResponse{data=interface{}}
// @Router /api/admin [post]
// @Security LoginToken
func (ac *adminController) AddAdmin(c *gin.Context) {
	var adminReq QO.AdminReq
	if err := c.ShouldBind(&adminReq); err != nil {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": err})
		return
	}
	err := ac.adminService.AddAdmin(c, adminReq)
	if err == nil {
		response.Success(c, "新增管理员数据成功", gin.H{})
	}
}

// DisabledAdmin 启用/禁用管理员
// @Summary 启用/禁用管理员
// @Description 启用/禁用管理员
// @Tags API.admin
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param id formData int true "Hashid"
// @Param used formData string true "是否启用"
// @Success 200 {object} response.TResponse{data=interface{}}
// @Failure 400 {object} response.TResponse{data=interface{}}
// @Router /api/admin/used [patch]
// @Security LoginToken
func (ac *adminController) DisabledAdmin(c *gin.Context) {
	var adminReq QO.AdminIdReq
	if err := c.ShouldBind(&adminReq); err != nil {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": err})
		return
	}
	fmt.Println("adminReq", adminReq)
	err := ac.adminService.DisabledAdmin(c, adminReq)
	if err == nil {
		response.Success(c, "下线管理员成功", gin.H{})
	}
}

// DeletedAdmin 删除管理员
// @Summary 删除管理员
// @Description 删除管理员
// @Tags API.admin
// @Accept json
// @Produce json
// @Param id path int true "hashId"
// @Success 200 {object} response.TResponse{data=interface{}}
// @Failure 400 {object} response.TResponse{data=interface{}}
// @Router /api/admin/{id} [delete]
// @Security LoginToken
func (ac *adminController) DeletedAdmin(c *gin.Context) {
	adminId := c.Param("id")
	if adminId == "" {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": "id 必填"})
		return
	}
	fmt.Println("cast.ToInt32(adminId)", cast.ToInt32(adminId))
	err := ac.adminService.DeletedAdmin(c, cast.ToInt32(adminId))
	if err == nil {
		response.Success(c, "删除管理员成功", gin.H{})
	}
}
